package task3;

import java.util.*;

public class test4 {//栈结构解决计算表达式

    static Scanner input=new Scanner(System.in);

    static Map<Character,Integer> priority=new HashMap<>();//储存符号优先级

    static Stack<Integer> num=new Stack<>();//数字栈
    static Stack<Character> op=new Stack<>();//符号栈


    public static void main(String[] args) {
        String content= input.nextLine();
        load();
        progress(content);
    }

    private static void load() {
        priority.put(')',0);
        priority.put('+',1);
        priority.put('-',1);
        priority.put('*',2);
        priority.put('/',2);
        priority.put('(',3);
    }

    private static void progress(String expression) {

        boolean inValidate=false;
        StringBuilder template=new StringBuilder("");

        outer:for(int i=0;i<expression.length();i++){
            char ch=expression.charAt(i);
            if(ch>='0'&&ch<='9'){//扫描数字，直到遇见下一个字符
                template.append(ch);
                if(i==expression.length()-1)
                    num.push(Integer.parseInt(template.toString()));
            }
            else{
                if(!template.toString().equals(""))//如果模板不为空的话
                    num.push(Integer.parseInt(template.toString()));//将模板里的数字类型转换并放入数字栈中


                if(!op.isEmpty()){//如果符号栈不为空的话

                    if(ch!='('&&ch!=')'){//不含括号的处理方式
                        if(inValidate=scan(ch)){
                            break outer;
                        }
                    }
                    else if(ch==')'){//遇见闭括号时
                        while (op.peek()!='('){//出栈括号内的所有元素
                            if(inValidate=!compute(op.peek()))
                                break outer;
                        }
                        op.pop();//开括号出栈
                    }

                }
                if(ch!=')')
                    op.push(ch);

                template=new StringBuilder("");//重置模板
            }
        }
        while (op.size()!=0){//最后的收尾计算
            if(inValidate=!compute(op.peek())){
                break;
            }
        }

        if(!inValidate){
            System.out.println(num.pop());
        }

    }

    private static boolean scan(char ch){
        boolean invalidate=false;

        char operator=op.peek();//获取符号栈顶元素
        int n1=priority.get(operator);
        int n2=priority.get(ch);

        if(operator!='('){//如果上一个符号为开括号，则无需进行计算
            while (n2<=n1){//若当前扫描的符号优先级低于等于栈顶符号，则计算出栈后入栈。
                if(invalidate=!compute(operator)){
                    break;
                }
                if(!op.isEmpty()){//符号不为空时更新符号
                    operator=op.peek();//重新获取符号栈顶元素
                    n1=priority.get(operator);
                }else {
                    break;
                }

            }
        }
        return invalidate;
    }
    private static boolean compute(char operator) {

        int n1=num.pop();
        int n2=num.pop();
        int result=0;
        boolean validate=true;

        switch (operator){
            case '+':{result=n2+n1;}break;
            case '-':{result=n2-n1;}break;
            case '*':{result=n2*n1;}break;
            case '/':{
                if(n1==0){
                    validate=false;
                    System.out.println("ILLEGAL");
                }
                else {
                    result=n2/n1;
                }
            }break;
        }
        if(validate){
            if(!op.isEmpty()){//符号栈不为空
                op.pop();
            }
            num.push(result);
        }
        return validate;
    }

}
